package services

import util.control.Breaks._
import util.control.Breaks._

case class IO (color: String)
case class AS (io: IO)
case class WS (name: String, inputs: Set[IO], outputs: Set[IO])

object ServicesTest extends App {

    val left     = Set(AS(IO("yellow")), AS(IO("pink")), 
                       AS(IO("green")), AS(IO("blue")))
    var right    = Set(AS(IO("orange")), AS(IO("green")), 
                       AS(IO("green")), AS(IO("purple")))
 
    val services = Set(
        WS( name    = "T:123",
            inputs  = Set(IO("yellow"), IO("pink"), IO("green")), 
            outputs = Set(IO("orange"), IO("green")) ),
        WS( name    = "T:234",
            inputs  = Set(IO("pink"), IO("green"), IO("blue")),
            outputs = Set(IO("green"), IO("purple")) )
    )

    val bank     = services
    var used     = Set.empty[WS]
    var added    = 0
    var phase    = 0
    var continue = true

    breakable { do {

        added  = 0
        phase += 1

        println("Phase %d".format(phase))

        def outputs = left.map(as => as.io) ++: used.flatMap(ws => ws.outputs)

        for (service <- bank -- used if service.inputs.subsetOf(outputs)) {
            println("Adding %s".format(service))
            used = used ++ Set(service)
            added += 1            
        }

        if (right.map(as => as.io) subsetOf used.flatMap(ws => ws.outputs)) break

    } while (added > 0) }

    println("done")
    
} // ServicesTest